package com.chess;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: a
 * Date: 2022-08-08
 * Time: 17:06
 */
public class ChessMainframe extends JFrame implements ActionListener, MouseListener, Runnable {
    //定义棋子, 共32颗棋子
    ChessManBehavior[] play = new ChessManBehavior[32];
    //定义棋盘
    JLabel image;
    //定义窗口内容显示对象
    Container con;
    //定义工具栏显示按钮
    JToolBar jmain;
    //定义按钮(新游戏, 悔棋, 退出)
    JButton anew, repent, exit;
    //定义文本提示框
    JLabel text;

    //定义变量(1为黑棋走; 2为红棋走; 3为双方都不能走)
    int chessPlayClick = 2;
    //判断是否点棋子
    boolean checkManClick;
    //点击棋子后的线程
    Thread tmain;
    //判断点击的棋子
    static int Man;

    /**
     * @param title 窗口的标题
     */
    public ChessMainframe(String title){
        con = this.getContentPane();
        //使用窗口绝对布局, 不会发生形状变化
        con.setLayout(null);
        //设置标题
        this.setTitle(title);
        //新建工具栏来存放这几个按钮
        jmain = new JToolBar();
        //显示文本提示内容
        text = new JLabel("欢迎来到象棋世界!");
        //设置点击按钮提示内容
        text.setToolTipText("信息提示:");
        //定义按钮
        anew = new JButton("新游戏");
        anew.setToolTipText("重新开始一局");
        repent = new JButton("悔棋");
        repent.setToolTipText("返回上次落子位置");
        exit = new JButton("退出");
        exit.setToolTipText("退出象棋对弈");
        //设置工具栏布局
        jmain.setLayout(new GridLayout(0, 4));
        //将按钮添加到工具栏中
        jmain.add(text);
        jmain.add(anew);
        jmain.add(repent);
        jmain.add(exit);
        //设置工具栏大小
        jmain.setBounds(0, 0, 558, 30);
        //将工具栏添加到内容面板
        con.add(jmain);
        //画棋子
        drawChessMain();
        for(int i = 0; i < 32; i++){
            con.add(play[i]);
            play[i].addMouseListener(this);
        }
        //添加背景图
        con.add(image = new JLabel(new ImageIcon(("image/main.gif"))));
        //设置背景大小
        image.setBounds(0, 30, 558, 620);
        image.addMouseListener(this);
        //居中
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation(screenSize.width/2-280, screenSize.height/2-350);
        //添加小图标
        this.setIconImage(new ImageIcon("image/红象.gif").getImage());
        //设置窗体不可改变大小
        this.setResizable(false);
        //设置窗体大小
        this.setSize(558, 676);
        //设置可见
        this.setVisible(true);
    }

    /**
     * 画棋盘 画棋子功能
     */
    public void drawChessMain(){
        int i, k;
        Icon icon = null;
        //初始化黑车
        icon = new ImageIcon("image/黑车.gif");
        for(i = 0, k = 24; i < 2; i++, k+=456){
            play[i] = new ChessManBehavior(icon);
            play[i].setBounds(k, 56, 55, 55);
            play[i].setName("车1");
        }
        //初始化黑马
        icon = new ImageIcon("image/黑马.gif");
        for(i = 4, k = 81; i < 6; i++, k+=342){
            play[i] = new ChessManBehavior(icon);
            play[i].setBounds(k, 56, 55, 55);
            play[i].setName("马1");
        }
        //初始化黑象
        icon = new ImageIcon("image/黑象.gif");
        for(i = 8, k = 138; i < 10; i++, k+=228){
            play[i] = new ChessManBehavior(icon);
            play[i].setBounds(k, 56, 55, 55);
            play[i].setName("象1");
        }
        //初始化黑士
        icon = new ImageIcon("image/黑士.gif");
        for(i = 12, k = 195; i < 14; i++, k+=114){
            play[i] = new ChessManBehavior(icon);
            play[i].setBounds(k, 56, 55, 55);
            play[i].setName("士1");
        }
        //初始化黑卒
        icon = new ImageIcon("image/黑卒.gif");
        for(i = 16, k = 24; i < 21; i++, k+=114){
            play[i] = new ChessManBehavior(icon);
            play[i].setBounds(k, 227, 55, 55);
            play[i].setName("卒1" + i);
        }
        //初始化黑炮
        icon = new ImageIcon("image/黑炮.gif");
        for(i = 26, k = 81; i < 28; i++, k+=342){
            play[i] = new ChessManBehavior(icon);
            play[i].setBounds(k, 170, 55, 55);
            play[i].setName("炮1" + i);
        }
        //初始化黑将
        icon = new ImageIcon("image/黑将.gif");
        play[30] = new ChessManBehavior(icon);
        play[30].setBounds(252, 56, 55, 55);
        play[30].setName("将1");

        //初始化红车
        icon = new ImageIcon("image/红车.gif");
        for(i = 2, k = 24; i < 4; i++, k+=456){
            play[i] = new ChessManBehavior(icon);
            play[i].setBounds(k, 569, 55, 55);
            play[i].setName("车2");
        }
        //初始化红马
        icon = new ImageIcon("image/红马.gif");
        for(i = 6, k = 81; i < 8; i++, k+=342){
            play[i] = new ChessManBehavior(icon);
            play[i].setBounds(k, 569, 55, 55);
            play[i].setName("马2");
        }
        //初始化红象
        icon = new ImageIcon("image/红象.gif");
        for(i = 10, k = 138; i < 12; i++, k+=228){
            play[i] = new ChessManBehavior(icon);
            play[i].setBounds(k, 569, 55, 55);
            play[i].setName("象2");
        }
        //初始化红士
        icon = new ImageIcon("image/红士.gif");
        for(i = 14, k = 195; i < 16; i++, k+=114){
            play[i] = new ChessManBehavior(icon);
            play[i].setBounds(k, 569, 55, 55);
            play[i].setName("士2");
        }
        //初始化红卒
        icon = new ImageIcon("image/红卒.gif");
        for(i = 21, k = 24; i < 26; i++, k+=114){
            play[i] = new ChessManBehavior(icon);
            play[i].setBounds(k, 398, 55, 55);
            play[i].setName("卒2" + i);
        }
        //初始化红炮
        icon = new ImageIcon("image/红炮.gif");
        for(i = 28, k = 81; i < 30; i++, k+=342){
            play[i] = new ChessManBehavior(icon);
            play[i].setBounds(k, 455, 55, 55);
            play[i].setName("炮2" + i);
        }
        //初始化红将
        icon = new ImageIcon("image/红将.gif");
        play[31] = new ChessManBehavior(icon);
        play[31].setBounds(252, 569, 55, 55);
        play[31].setName("将2");
    }

    @Override
    public void actionPerformed(ActionEvent e) {

    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if(tmain == null){
            tmain = new Thread(this);
            tmain.start();
        }
        //判断点击的是棋盘还是棋子
        if(e.getSource().equals(image)){

        }else{
            int iClick = 0;
            for(int i = 0; i < 32; i++){
                if(e.getSource().equals(play[i])){
                    iClick = i;
                    break;
                }
            }
            //红黑棋走判断
            if(chessPlayClick == 2){
                //点击红棋
                if(play[iClick].getName().charAt(1) == '2'){
                    Man = iClick;
                    checkManClick = true;
                    return;
                }
                //红棋吃子判断
                int judge = play[Man].getName().charAt(1) - '0';
                if(play[iClick].getName().charAt(1) == '1' && judge == 2){

                }
            }else if(chessPlayClick == 1){
                if(play[iClick].getName().charAt(1) == '1'){
                    Man = iClick;
                    checkManClick = true;
                    return;
                }
                //黑七吃子判断
                int judge = play[Man].getName().charAt(1) - '0';
                if(play[iClick].getName().charAt(1) == '2' && judge == 1){

                }
            }
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }

    @Override
    public void run() {
        while(true){
            if(checkManClick){
                play[Man].setVisible(false);
                try {
                    tmain.sleep(400);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                play[Man].setVisible(true);
                for(int i = 0; i < 32; i++){
                    if(i != Man && play[i].live == true){
                        play[i].setVisible(true);
                    }
                }
            }else{
                text.setVisible(false);
                try {
                    tmain.sleep(250);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                text.setVisible(true);
            }
            try {
                tmain.sleep(550);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
